Programming Questions for Practice
Write a program to rotate the given array towards right by specified number of places.
#include <stdio.h>
#include <stdlib.h>
void displayArray(int array[], int length) {
printf("Array is\n");
for(int i = 0; i < length; i++)
printf("%d ", array[i]);
}
int main()
{
// Creating an array of given length
int length;
printf("Enter the length of the array: ");
scanf("%d", &length);
int *array = (int *)malloc(length*sizeof(int));
printf("Enter any %d numbers: ", length);
for(int i = 0; i < length; i++)
scanf("%d", &array[i]);
displayArray(array, length);
// Reading the number places to be rotated
int places;
printf("\nEnter the number of places: ");
scanf("%d", &places);
// Logic of rotation towards right
for(int counter = 1; counter<=places; counter++) {
int temp = array[length-1], i;
for(i=length-1; i >= 0; i--) {
array[i] = array[i-1];
}
array[i+1] = temp;
}
// Display the result
printf("\nAfter the right rotation by %d positions\n", places);
displayArray(array, length);
return 0;
}
Code not available...
Code not available...